<?php
namespace Admin\Controller;
use Think\Controller;
class AdController extends CommonController {
    public function lst(){
		$ad=D('ad');
		$count      = $ad->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$Page->setConfig('first','首页');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('end','共有{%TOTAL_PAGE%}页');
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$lis= $ad->field('a.*,b.pname')->alias('a')->JOIN('LEFT JOIN sp_adpos b ON a.posid=b.id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('lis',$lis);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display('lst');
	}
	
	
	public function del(){
		$ad=D('ad');
		if($ad->delete(I('id'))){
			$this->success("删除广告成功！",U('lst'));
		}else{
			$this->error("删除广告失败！");
		}
	}
	

	
	public function add() {
				$ad = D('ad'); //实例化数据库
				if (IS_POST) {
						//添加数据if($_FILES['picurl'][tmp_name]){
						 $upload = new \Think\Upload();// 实例化上传类
						 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
						 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
						 $upload->savePath  =      './Public/Uploads/Ad/'; // 设置附件上传目录
						 $upload->rootPath  =      './'; // 设置上传根目录
						// $upload->autoSub   =     false;// 设置图片上传不生成子目录
						 $info   =   $upload->uploadOne($_FILES['picurl']); 
						 $picurl=$info['rootPath'] . $info['savepath'] . $info['savename']; 
						 $_POST['picurl']=$picurl;
						 $ad->create(); //收集post表单数据
						// dump($_POST);die;
						if ($ad->add()!==false) {
									$this->success('广告添加成功！', U('lst'));
						} else {
									$this -> error('广告添加失败！', U('add'));
						}
						return;
				} 
				$adpos=D('adpos')->select();
				$this->assign('adpos',$adpos);
				$this->display(); //没有POST显示空模板
	}
	
	
	
	public function edit(){
			$ad=D('ad');
			if(IS_POST){
					if($_POST['ison'] =='on'){
						$_POST['ison']=1;
					}else{
						$_POST['ison']=0;
					}
				//dump($_POST);die;
				 $upload = new \Think\Upload();// 实例化上传类
					 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
					 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
					 $upload->savePath  =      './Public/Uploads/Ad/'; // 设置附件上传目录
					 $upload->rootPath  =      './'; // 设置附件上传目录
					// $upload->autoSub   =     false;// 设置图片上传不生成子目录
					 $info   =   $upload->uploadOne($_FILES['picurl']); 
						 $original=$info['rootPath'] . $info['savepath'] . $info['savename']; 
						 $_POST['picurl']=$original;
				if($ad->create()){
					if($ad->save()!==false){
						$this->success("修改广告成功！",U('lst'));
					}else{
						$this->error("修改广告失败！");
					}
				}
				return ;
			}
			$adpos=D('adpos')->select();
			$this->assign('adpos',$adpos);
			$id=I('id');
			$ads = $ad->find($id);
			if($ads['type'] == 2){
				$adpic = D('adpic')->where(array('adid'=>$id))->select();
				$this->assign('adpic',$adpic);
			}
			$this->assign("ads",$ads);
			$this->display();
		
	}
	
	public function ajaxdeladpic($id){
		$adpic=D('adpic');
		$adpics=$adpic->find($id);
		if(file_exists($adpics['imgurl'])){
			unlink($adpics['imgurl']);
		}
		$adpic->delete($id);
	}
}